package com.fastloan.jn.core.handler;

import com.kawins.shiro.temp.achieve.hander.ShiroExceptionHandler;
import com.kawins.shiro.temp.core.expetion.LoginCodeErrorException;
import com.kawins.shiro.temp.core.expetion.LoginCodeOutException;
import com.kawins.shiro.temp.core.result.ShiroResult;
import com.kawins.shiro.temp.core.result.ShiroResultEnum;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/20 10:15
 */
@RestControllerAdvice
public class GlobalExceptionHandler implements ShiroExceptionHandler {

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public ShiroResult shiroException(UnauthorizedException ex) {
        return new ShiroResult(ShiroResultEnum.NOT_AUTHORIZED);
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public ShiroResult shiroException(UnknownAccountException ex) {
        return new ShiroResult(ShiroResultEnum.UNKNOWN_ACCOUNT);
    }

    @Override
    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseStatus(HttpStatus.OK)
    public ShiroResult shiroException(IncorrectCredentialsException ex) {
        return new ShiroResult(ShiroResultEnum.INCORRECT_CREDENTIALS);
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public ShiroResult shiroException(LockedAccountException ex) {
        return new ShiroResult(ShiroResultEnum.LOCKED_ACCOUNT);
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public ShiroResult shiroException(DisabledAccountException ex) {
        return new ShiroResult(ShiroResultEnum.DISABLED_ACCOUNT);
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public ShiroResult shiroException(LoginCodeErrorException ex) {
        return new ShiroResult(ShiroResultEnum.LOGIN_CODE_ERROR);
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public ShiroResult shiroException(LoginCodeOutException ex) {
        return new ShiroResult(ShiroResultEnum.LOGIN_CODE_TIMEOUT);
    }
}
